﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.Entity.Infrastructure;
using System.Data.Entity;

using SchoolSystem.DAL;
using Theoreme.Library;

namespace SchoolSystem.UI.ConsoleHelper
{
    public class Program
    {
        static void Main(string[] args)
        {
            //ListAllContries();
            //DisplayListOfCourses();

            //Test_WriteDerivedEntities();
            //Test_LoadDerivedEntities();
            AddTeacher_WithAllProgrammingCourses();
        }

        /// <summary>
        /// Fills the countries table.
        /// </summary>
        private static void FillCountriesTable()
        {
            string fileName = @"C:\Users\Sebastien\Documents\Code\@SchoolSystem Application\SchoolSystem\@Ressources\CountryList.csv";

            using (var context = new SchoolSystemDbEntities())
            {

                var query = (from line in File.ReadAllLines(fileName)
                             let parts = line.Split(';')
                             let country = new Country()
                             {
                                 Name = parts[0],
                                 IsoCode3166 = parts[1],
                             }
                             select country).ToList();

                foreach (var country in query)
                {
                    context.Countries.Add(country);
                    
                }
                context.SaveChanges();

            }
        }

        /// <summary>
        /// Displays the list of courses.
        /// </summary>
        private static void DisplayListOfCourses()
        {
            using (var context = new SchoolSystemDbEntities())
            {

                var query = from course in context.Courses.Include("Category")
                            select course;

                
                query.ToList().ForEach( x => Console.WriteLine( x.Category.Name));
                //foreach (var item in query)
                //{
                //    Console.WriteLine(item.Category.Name);
                    
                //}

                


                query = from course in context.Courses
                        where course.Category.Name == "Programming"
                        select course;

                foreach (var item in query)
                {
                    Console.WriteLine(item.Name);
                }

            
            
            }


            

        }


        private static void Test_WriteDerivedEntities()
        {
            using (var db = new SchoolSystemDbEntities())
            {

                // Create some teachers..
                for (int i = 0; i < 5; i++)
                {
                    DerivedTeacher t = new DerivedTeacher()
                    {
                        Name = "Teacher1" + i.ToString(),
                        HireDate = DateTime.Now,
                    };

                    db.BasePersons.Add(t);
                    
                }

                // Create some teachers..
                for (int i = 0; i < 5; i++)
                {
                    DerivedStudent t = new DerivedStudent()
                    {
                        Name            = "Student" + i.ToString(),
                        EnrollmentDate  = DateTime.Now,
                    };

                    db.BasePersons.Add(t);

                }



                db.SaveChanges();
            }




        }


        private static void Test_LoadDerivedEntities()
        {
            using (var db = new SchoolSystemDbEntities())
            {
                var query = from person in db.BasePersons
                            select person;
                Console.WriteLine("All People: " + query.Count().ToString());

                var query1 = from student in db.BasePersons.OfType<DerivedStudent>()
                             select student;
                Console.WriteLine("Students: " + query1.Count().ToString());

                var query2 = from profesor in db.BasePersons.OfType<DerivedTeacher>()
                             select profesor;
                Console.WriteLine("Professors: " + query2.Count().ToString());


                var query3 = from teacher in db.Teachers
                             select teacher;

                Console.WriteLine("Teachers via DbSet<Teachers>: " + query3.Count().ToString());


                var query4 = from student in db.Students
                             select student;

                Console.WriteLine("Student via DbSet<Student>: " + query4.Count().ToString());


            }
        }


        private static void AddTeacher_WithAllProgrammingCourses()
        {
            using (var db = new SchoolSystemDbEntities())
            {


                var query = from t in db.Teachers.Include("Adress").Where(t => t.Address.Country.Name == "France").Select(t => new { MainPhone = t.HomePhone})
                            select t;

                //var teacher = new Teacher()
                //{
                //    FirstName = "FakeFirstName",
                //    LastName = "FakeLastName" + DateTime.Now.Millisecond.ToString(),
                //    BirthDate = new DateTime(1970, 8, 21),
                //    Courses = db.Courses.Where(x => x.Category.Name == "Programming"),
                //}; 

                //ObjectDumper.Write(teacher, 2);



                //var courses = db.Courses.Where(x => x.Category.Name == "Programming").ToList();

                // teacher.Courses.AddList(courses);

                //db.Teachers.Add(teacher);
                //db.SaveChanges();

                //foreach (var course in teacher.Courses)
                //{
                //    Console.WriteLine(course.Category.Name);
                //    Console.WriteLine(course.Name);
                //}


                //(from aTeacher in db.Teachers.Include("Courses")
                //             where aTeacher.Courses.Count > 0
                //             select aTeacher).ToList().ForEach(x => ObjectDumper.Write(x ));


            }
        }


    }   // End Class

   

}       // End Namespace
